package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import dao.UserDAO;
import entity.User;
import tools.BaseDAO;

public class UserDAOImpl extends BaseDAO implements UserDAO{

	@Override
	public void addUser(User u) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_user values(null,?,?)";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, u.getName());
			ps.setString(2, u.getPass());
			ps.executeUpdate();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
	}

	@Override
	public int checkUser(String name,String pass) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int uid = 0;
		String sql = "select * from tbl_user where name=? and pass=?";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, pass);
			rs = ps.executeQuery();
			if (rs.next()) {
				uid = rs.getInt(1);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
		return uid;
	}
	
}
